package com.cjw.campus.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.core.result.JsonResult;
import com.cjw.campus.core.utils.UploadUtil;
import com.cjw.campus.system.entity.QuestionsTable;
import com.cjw.campus.system.entity.ReplyQuestionsTable;
import com.cjw.campus.system.service.IQuestionsTableService;
import com.cjw.campus.system.service.IReplyQuestionsTableService;
import com.cjw.campus.system.vo.QuestionsTableVO;
import com.cjw.campus.system.vo.ReplyQuestionsTableVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * 回答问题表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2023-03-21
 */
@RestController
@RequestMapping("/system/replyQuestionsTable")
@Tag(name = "回答问题管理接口")
public class ReplyQuestionsTableController {

    @Autowired
    private IReplyQuestionsTableService iReplyQuestionsTableService;

    @Autowired
    private IQuestionsTableService iQuestionsTableService;

    @GetMapping("/getDescPageReplyQuestionsByQuestionsId")
    @Operation(summary = "根据id倒叙分页查询回答问题信息", security = { @SecurityRequirement(name = "Authorization")})
    @Parameters({
            @Parameter(name = "questionsId", description = "问题id"),
            @Parameter(name = "currentPage", description = "当前页"),
            @Parameter(name = "pageSize", description = "页数")
    })
    public JsonResult getDescPageReplyQuestionsByQuestionsId (Integer questionsId, Integer currentPage, Integer pageSize) {
        IPage<QuestionsTableVO> page = new Page<>(currentPage, pageSize);
        IPage<ReplyQuestionsTableVO> replyQuestions = iReplyQuestionsTableService.getDescPageReplyQuestionsByQuestionsId(questionsId, page);
        return JsonResult.ok(replyQuestions);
    }

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public JsonResult upload (@RequestPart("file") MultipartFile file) {
        String path = UploadUtil.uploadFile(file);
        return JsonResult.ok(path);
    }

    @PostMapping("/save")
    @Operation(summary = "保存回答问题信息", security = { @SecurityRequirement(name = "Authorization")})
    @Parameter(name = "replyQuestionsTable")
//    public JsonResult save (ReplyQuestionsTable replyQuestionsTable) {
    public JsonResult save (@RequestBody ReplyQuestionsTable replyQuestionsTable) {
        // 保存问题信息
        boolean save = iReplyQuestionsTableService.save(replyQuestionsTable);
        // 判断用户是否新增成功
        if (save) {

            // 实例化对象
            QueryWrapper<QuestionsTable> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(QuestionsTable::getQuestionsId, replyQuestionsTable.getQuestionsId());
            QuestionsTable questionsTable = iQuestionsTableService.getOne(queryWrapper);

            QuestionsTable questionsTables = new QuestionsTable();
            // 设置问题id为新增时输入的id，用于根据id修改回答数
            questionsTables.setQuestionsId(replyQuestionsTable.getQuestionsId());
            if (questionsTable.getReplyNumber() == null) {
                questionsTables.setReplyNumber(1);
            } else {
                questionsTables.setReplyNumber(questionsTable.getReplyNumber() + 1);
            }
            iQuestionsTableService.updateById(questionsTables);

            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

}
